package com.hsy.directseeding.main;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.util.TypedValue;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.deparse.dpframe.ui.widget.DPViewPager;
import com.deparse.dpframe.ui.widget.DPagerSlidingTabStrip;
import com.deparse.dpframe.utils.DPGsonUtil;
import com.deparse.dpframe.utils.DPHttpUtil;
import com.deparse.dpframe.utils.DPPicassoUtil;
import com.deparse.dpframe.utils.DPUnitUtil;
import com.hsy.directseeding.R;
import com.hsy.directseeding.common.CommonBaseActivity;
import com.hsy.directseeding.common.Consts;
import com.hsy.directseeding.main.fragment.UAVFragment;
import com.hsy.directseeding.main.fragment.USFragment;
import com.hsy.directseeding.my.MyActivity;
import com.hsy.directseeding.utils.CommonUtils;
import com.hsy.directseeding.utils.HttpUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2016/9/9.
 */
public class MainActivity extends CommonBaseActivity {
    private DPViewPager activity_repair_viewpager;
    private List<Fragment> myFragmentList;
    private DPagerSlidingTabStrip tabs;
    private MyPagerAdapter adapter;
    private ImageView to_my_image;

    private DPViewPager.OnPageChangeListener onPageChangeListener = new DPViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int i, float v, int i1) {

        }

        @Override
        public void onPageSelected(int i) {
            if (i == 0) {
                ((TextView) ((ViewGroup) tabs.getChildAt(0)).getChildAt(0)).setTextAppearance(MainActivity.this, R.style.login_pagerSlidingTab_text_true);
                ((TextView) ((ViewGroup) tabs.getChildAt(0)).getChildAt(1)).setTextAppearance(MainActivity.this, R.style.login_pagerSlidingTab_text_false);
            } else {
                ((TextView) ((ViewGroup) tabs.getChildAt(0)).getChildAt(0)).setTextAppearance(MainActivity.this, R.style.login_pagerSlidingTab_text_false);
                ((TextView) ((ViewGroup) tabs.getChildAt(0)).getChildAt(1)).setTextAppearance(MainActivity.this, R.style.login_pagerSlidingTab_text_true);
            }
        }

        @Override
        public void onPageScrollStateChanged(int i) {

        }
    };
    private DPHttpUtil.HttpCallback callBack = new DPHttpUtil.HttpCallback() {
        @Override
        public void onSuccess(int what, String response) {
            MainListGson.Result result = DPGsonUtil.fromJson(response, MainListGson.Result.class);
            DPPicassoUtil.with(MainActivity.this).load(result.getC_logo()).error(R.mipmap.touxciang).into(to_my_image);
            CommonUtils.hideProgressDialog();
        }

        @Override
        public void onError(int what, int type, VolleyError error) {

        }
    };

    @Override
    protected void onResume() {
        super.onResume();
        Map<String, String> map = new HashMap<String, String>();
        map.put("token", CommonUtils.getToken(this));
        HttpUtil.httpPostWithParams(this, 0, Consts.Apis.POST_MAIN_LIST, map, callBack);
    }


    private static boolean isExit = false;
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            isExit = false;
        }
    };

    private void exit() {

        if (!isExit) {
            isExit = true;
            showToast("再按一次离开");
            // 利用handler延迟发送更改状态信息
            handler.sendEmptyMessageDelayed(0, 2000);
        } else {
            finishAllActivities();
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (keyCode == KeyEvent.KEYCODE_BACK) {
            exit();
            return false;
        }
        return super.onKeyDown(keyCode, event);

    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getActionBarContainer().setVisibility(View.GONE);

        setContentView(R.layout.activity_main);

        activity_repair_viewpager = F(R.id.activity_repair_viewpager);

        to_my_image = F(R.id.to_my_image);

        tabs = F(R.id.tabs);

        tabs.setIndicatorHeight(2);

        tabs.setIndicatorColorResource(R.color.white);

        tabs.setIndicatorWidth(DPUnitUtil.dip2px(this, 50));

        tabs.setOnPageChangeListener(onPageChangeListener);

        showTab();

        to_my_image.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(MyActivity.class);
            }
        });


    }

    private void showTab() {
        myFragmentList = new ArrayList<Fragment>();
        UAVFragment uavFragment = new UAVFragment();
        USFragment usFragment = new USFragment();
        uavFragment.setAttchedActivity(this);
        usFragment.setAttchedActivity(this);
        myFragmentList.add(uavFragment);
        myFragmentList.add(usFragment);

        adapter = new MyPagerAdapter(getSupportFragmentManager(), myFragmentList);


        int pageMargin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 4, getResources().getDisplayMetrics());
        activity_repair_viewpager.setAdapter(adapter);
        activity_repair_viewpager.setPageMargin(pageMargin);
        tabs.setViewPager(activity_repair_viewpager);

        onPageChangeListener.onPageSelected(0);
    }

    private class MyPagerAdapter extends FragmentPagerAdapter {

        private final String[] TITLES = {"无人机", "无人船"};
        private List<Fragment> myFragmentsList;

        public MyPagerAdapter(FragmentManager fragmentmanager, List<Fragment> list) {
            super(fragmentmanager);
            myFragmentsList = list;
        }

        public int getItemPosition(Object obj) {
            return super.getItemPosition(obj);
        }

        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        public int getCount() {
            return myFragmentsList.size();
        }

        public Fragment getItem(int i) {
            return (Fragment) myFragmentsList.get(i);
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return TITLES[position];
        }

    }
}
